﻿using System;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;
using DevExpress.XtraEditors;
using ScanLibCtl;

namespace ImagingScan
{
    public partial class ScanningCtrl : XtraUserControl
    {
        public ScanningCtrl()
        {
            InitializeComponent();
        }
        public string NEW_SCANNED_FILE_PATH
        { get; set; }

        public bool StartScan()
        {
            bool isScannedCompleted = false;
            try
            {
                if (!axImgScan1.ScannerAvailable())
                {
                    MessageBox.Show("there is no twain scanners available");
                }

                var sett = new ScanningSettings();// GetSettings();
                //if (sett != null)
                //{
                //    if (sett.IsShowSelectScanner)
                //        axImgScan1.SetScannerName(axImgScan1.DefaultScanner);
                //    else
                //        axImgScan1.SetScannerName(sett.ScannerName);

                //    var loc = Path.GetTempPath();
                //    axImgScan1.Image = Path.Combine(loc, Guid.NewGuid().ToString().Replace("-", "") + ".tiff");
                //    axImgScan1.CompressionType = CompressionTypeConstants.JPEG;

                //    //axImgScan1.SetPageTypeCompressionOpts(CompPreferenceConstants.BestDisplay,
                //    //    ImageTypeConstants.BlackAndWhite1Bit,
                //    //    CompTypeConstants.JPEGCompression,
                //    //    CompInfoConstants.JPEGMedMed);

                //    axImgScan1.FileType = FileTypeConstants.TIFF;

                //    if (sett.IsMultiPage)
                //    {
                //        axImgScan1.MultiPage = true;
                //        axImgScan1.PageOption = PageOptionConstants.AppendPages;
                //    }
                //    else
                //    {
                //        axImgScan1.MultiPage = true;
                //        axImgScan1.PageOption = PageOptionConstants.CreateNewFile;
                //    }

                //    axImgScan1.StopScanBox = true;
                //    axImgScan1.ShowSetupBeforeScan = false;
                //    int res = axImgScan1.StartScan();
                //    if (res != 0)
                //        MessageBox.Show("Unknown scanning error");
                //}
                //else
                {
                    axImgScan1.ShowSelectScanner();
                    string x = axImgScan1.SelectedScanner;
                    var loc = Path.GetTempPath();
                    NEW_SCANNED_FILE_PATH = Path.Combine(loc, Guid.NewGuid().ToString().Replace("-", "") + ".tiff");
                    axImgScan1.Image = NEW_SCANNED_FILE_PATH;

                    axImgScan1.SetPageTypeCompressionOpts(CompPreferenceConstants.BestDisplay,
                        ImageTypeConstants.BlackAndWhite1Bit,
                        CompTypeConstants.JPEGCompression,
                        CompInfoConstants.JPEGMedMed);

                    axImgScan1.FileType = FileTypeConstants.TIFF;

                    axImgScan1.MultiPage = true;
                    axImgScan1.PageOption = PageOptionConstants.CreateNewFile;

                    axImgScan1.PageType = PageTypeConstants.BlackAndWhite;
                    axImgScan1.StopScanBox = true;
                    axImgScan1.ShowSetupBeforeScan = false;
                    int res = axImgScan1.StartScan();

                    axImgEdit1.Image = NEW_SCANNED_FILE_PATH;
                    axImgEdit1.Display();
                    axImgEdit1.FitTo(1);
                    axImgEdit1.Refresh();
                    isScannedCompleted =  true;
                }
            }
            catch (Exception ex)
            {
                if (ex.Message == "TWAIN Data Source not found. Please reinstall scanner software.")
                {
                    MessageBox.Show("The provided Scanner name not valid or There is no Twain Scanner found");
                }
            }
            return isScannedCompleted;
        }

        //public static void SaveSettings(ScanningSettings settings)
        //{
        //    //save the settings
        //    try
        //    {
        //        var serializer = new XmlSerializer(typeof(ScanningSettings));
        //        using (var wr = new StreamWriter("Settings.xml", false))
        //        {
        //            serializer.Serialize(wr, settings);
        //            wr.Close();
        //            wr.Dispose();
        //            serializer = null;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //    }
        //}

        //public static ScanningSettings GetSettings()
        //{
        //    //get the settings
        //    try
        //    {
        //        var serializer = new XmlSerializer(typeof(ScanningSettings));
        //        using (var reader = new StreamReader("Settings.xml"))
        //        {
        //            var res = (ScanningSettings)serializer.Deserialize(reader);
        //            reader.Close();
        //            reader.Dispose();
        //            serializer = null;
        //            return res;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        return null;
        //    }
        //}

        public static void ShowScanningSetting()
        {
            var frm = new GetSettings();
            frm.ShowDialog();
        }
    }


    public class ScanningSettings
    {
        public string ScannerName
        {
            get { return Properties.ScanningSettings.Default.ScannerName; }
            set
            {
                Properties.ScanningSettings.Default.ScannerName = value;
                Properties.ScanningSettings.Default.Save();
            }
        }

        public bool IsMultiPage
        {
            get { return Properties.ScanningSettings.Default.IsMultiPage; }
            set
            {
                Properties.ScanningSettings.Default.IsMultiPage = value;
                Properties.ScanningSettings.Default.Save();
            }
        }

        public bool IsShowSelectScanner
        {
            get { return Properties.ScanningSettings.Default.IsShowSelectScanner; }
            set
            {
                Properties.ScanningSettings.Default.IsShowSelectScanner = value;
                Properties.ScanningSettings.Default.Save();
            }
        }

        public bool IsColor
        {
            get { return Properties.ScanningSettings.Default.IsColor; }
            set
            {
                Properties.ScanningSettings.Default.IsColor = value;
                Properties.ScanningSettings.Default.Save();
            }
        }

    }
}